package com.example.analyzevisio.service;

import com.example.analyzevisio.model.Judge;
import com.example.analyzevisio.model.component.Position;
import com.example.analyzevisio.model.component.Size;
import com.example.analyzevisio.model.judge.*;
import com.example.analyzevisio.model.judge.drop.Circle;
import com.example.analyzevisio.model.judge.drop.Style;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.example.analyzevisio.utis.Obtain.*;

public class JudgeService {
    private static HashMap<String, Map<String, String>> dataMap;




    public void setDataMap(HashMap<String, Map<String, String>> dataMap){
        this.dataMap = dataMap;
    }

    /**
     * 判断节点
     * @param shapeElement
     * @param textNodes
     * @return
     */
    public static Judge getJudge(Element shapeElement, NodeList textNodes) {
        Judge judge = new Judge();

        //获取坐标和父节点
        Map<String, String> obtainCoordinates = getObtainCoordinates(shapeElement);
        double pinX = Double.parseDouble(obtainCoordinates.get("pinX"));
        double pinY = Double.parseDouble(obtainCoordinates.get("pinY"));

        judge.setPosition(new Position(pinX,pinY));
        judge.setParent(obtainCoordinates.get("parent"));



        Size size = new Size(
                60,
                20
        );

        judge.setSize(size);
        Body body = new Body("0,10 10,0 20,10 10,20", "rgb(102, 0, 255)");
        //获取文本
        String text = getText(textNodes);
        Label label = new Label(text, "#333", "8");
        judge.setAttrs(new Attrs(body,label));
        judge.setShape("custom-polygon");

        Ports ports = new Ports();
        Groups groups = new Groups();

        Drop topGroups = new Drop();
        topGroups.setPosition("top");
        com.example.analyzevisio.model.judge.drop.Attrs attrs = new com.example.analyzevisio.model.judge.drop.Attrs();
        Circle circle = new Circle();
        circle.setR("4");
        circle.setMagnet(true);
        circle.setStroke("#D06269");
        circle.setStrokeWidth("1");
        circle.setFill("#fff");
        circle.setStyle(new Style("hidden"));
        attrs.setCircle(circle);
        topGroups.setPosition("top");
        topGroups.setAttrs(attrs);
        groups.setTop(topGroups);

        Drop rightGroups = new Drop();
        rightGroups.setPosition("right");
        rightGroups.setAttrs(attrs);
        groups.setRight(rightGroups);

        Drop bottomGroups = new Drop();
        bottomGroups.setPosition("bottom");
        bottomGroups.setAttrs(attrs);
        groups.setBottom(bottomGroups);

        Drop leftGroups = new Drop();
        leftGroups.setPosition("left");
        leftGroups.setAttrs(attrs);
        groups.setLeft(leftGroups);



        ArrayList<Items> itemsArrayList = new ArrayList<>();
        Items top = new Items();
        top.setGroup("top");
        top.setId("port3");
        itemsArrayList.add(top);

        Items right = new Items();
        right.setGroup("right");
        right.setId("port2");
        itemsArrayList.add(right);

        Items bottom = new Items();
        bottom.setGroup("bottom");
        bottom.setId("port4");
        itemsArrayList.add(bottom);

        Items left = new Items();
        left.setGroup("left");
        left.setId("port1");
        itemsArrayList.add(left);

        ports.setGroups(groups);
        ports.setItems(itemsArrayList);

        judge.setPorts(ports);

        String uniqueID = shapeElement.getAttribute("UniqueID");
        String substring = uniqueID.substring(1, uniqueID.length() - 1);
        judge.setId(substring);

        JudgeData judgeData = new JudgeData();
        judgeData.setLabel(text);
        String shapeID = shapeElement.getAttribute("ID");
        judgeData.setStateId(dataMap.get(shapeID).get("stateId"));
        judgeData.setStateType("Choice");
        judgeData.setDesc("条件");
        judge.setData(judgeData);

        judge.setZIndex("10");



        return judge;
    }

}
